<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>PostMessage / SendMessage</title>
<meta name="description" content="Send messages to a window with this free macro program. PostMessage puts a message in the window's message queue, but SendMessage waits for a reply.">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>PostMessage / SendMessage</h1>

<p>Sends a message to a window or control (SendMessage additionally waits for acknowledgement).</p>

<pre class="Syntax">PostMessage, Msg [, wParam, lParam, Control, WinTitle, WinText, ExcludeTitle, ExcludeText]
SendMessage, Msg [, wParam, lParam, Control, WinTitle, WinText, ExcludeTitle, ExcludeText, Timeout]</pre>
<h3>Parameters</h3>
<dl>

  <dt>Msg</dt>
  <dd><p>The message number to send, which can be an <a href="../Variables.htm#Expressions">expression</a>. See the <a href="../misc/SendMessageList.htm">message list</a> to determine the number.</p></dd>

  <dt>wParam</dt>
  <dd><p>The first component of the message, which can be an <a href="../Variables.htm#Expressions">expression</a>. If blank or omitted, 0 will be sent.</p></dd>

  <dt>lParam</dt>
  <dd><p>The second component of the message, which can be an <a href="../Variables.htm#Expressions">expression</a>.  If blank or omitted, 0 will be sent.</p></dd>

  <dt>Control</dt>
  <dd><p>If this parameter is blank or omitted, the message will be sent directly to the target window rather than one of its controls. Otherwise, this parameter can be either ClassNN (the classname and instance number of the control) or the control's text, both of which can be determined via Window Spy. When using text, the matching behavior is determined by <a href="SetTitleMatchMode.htm">SetTitleMatchMode</a>.</p>
    <p>To operate upon a control's HWND (window handle), leave the <em>Control</em> parameter blank and specify <code>ahk_id %ControlHwnd%</code> for the <em>WinTitle</em> parameter (this also works on hidden controls even when <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a> is Off). The HWND of a control is typically retrieved via <a href="ControlGet.htm#Hwnd">ControlGet Hwnd</a>, <a href="MouseGetPos.htm">MouseGetPos</a>, or <a href="DllCall.htm">DllCall</a>.</p>
    </dd>

  <dt>WinTitle</dt>
  <dd><p>A window title or other criteria identifying the target window. See <a href="../misc/WinTitle.htm">WinTitle</a>.</p></dd>

  <dt>WinText</dt>
  <dd><p>If present, this parameter must be a substring from a single text element of the target window (as revealed by the included Window Spy utility). Hidden text elements are detected if <a href="DetectHiddenText.htm">DetectHiddenText</a> is ON.</p></dd>

  <dt>ExcludeTitle</dt>
  <dd><p>Windows whose titles include this value will not be considered.</p></dd>

  <dt>ExcludeText</dt>
  <dd><p>Windows whose text include this value will not be considered.</p></dd>

  <dt>Timeout</dt>
  <dd><p><span class="ver">[AHK_L 42+]:</span> The maximum number of milliseconds to wait for the target window to process the message. If omitted, it defaults to 5000 (milliseconds), which is also the default behaviour in older versions of AutoHotkey which did not support this parameter. If the message is not processed within this time, the command finishes and sets ErrorLevel to the word FAIL. This parameter can be an <a href="../Variables.htm#Expressions">expression</a>.</p></dd>

</dl>

<h3>ErrorLevel</h3>
<p><span class="ver">[v1.1.04+]</span> These commands are able to throw an exception on failure. For more information, see <a href="Catch.htm#RuntimeErrors">Runtime Errors</a>.</p>
<p>PostMessage: <a href="../misc/ErrorLevel.htm">ErrorLevel</a> is set to 1 if there was a problem such as the target window or control not existing. Otherwise, it is set to 0.</p>
<p>SendMessage: <a href="../misc/ErrorLevel.htm">ErrorLevel</a> is set to the word FAIL if there was a problem or the command timed out. Otherwise, it is set to the numeric result of the message, which might sometimes be a &quot;reply&quot; depending on the nature of the message and its target window.</p>
<p>The range of possible values depends on the target window and the version of AutoHotkey that is running.  When using a 32-bit version of AutoHotkey, or if the target window is 32-bit, the result is a 32-bit unsigned integer between 0 and 4294967295. When using the 64-bit version of AutoHotkey with a 64-bit window, the result is a 64-bit signed integer between -9223372036854775808 and 9223372036854775807.</p>
<p>If the result is intended to be a 32-bit signed integer (a value from -2147483648 to 2147483648), it can be truncated to 32-bit and converted to a signed value as follows:</p>
<pre>MsgReply := ErrorLevel &lt;&lt; 32 &gt;&gt; 32</pre>
<p>This conversion may be necessary even on AutoHotkey 64-bit, because results from 32-bit windows are zero-extended. For example, a result of -1 from a 32-bit window is seen as 0xFFFFFFFF on any version of AutoHotkey, whereas a result of -1 from a 64-bit window is seen as 0xFFFFFFFF on AutoHotkey 32-bit and -1 on AutoHotkey 64-bit.</p>

<h3>Remarks</h3>
<p>These commands should be used with caution because sending a message to the wrong window (or sending an invalid message) might cause unexpected behavior or even crash the target application. This is because most applications are not designed to expect certain types of messages from external sources.</p>
<p>PostMessage places the message in the message queue associated with the target window. It does not wait for acknowledgement or reply. By contrast, SendMessage waits for the target window to process the message, up until the timeout period expires.</p>
<p>The <em>wParam</em> and <em>lParam</em> parameters should be integers. If AutoHotkey or the target window is 32-bit, only the low 32 bits are used; that is, the value should be between -2147483648 and 4294967295 (0xFFFFFFFF). If AutoHotkey and the target window are both 64-bit, any integer value <a href="../Variables.htm#cap">supported by AutoHotkey</a> can be used. As with all integer values in AutoHotkey, a prefix of 0x indicates a hex value. For example, 0xFF is equivalent to 255.</p>
<p>A string may be sent via <em>wParam</em> or <em>lParam</em> by specifying the address of a variable. The following example uses the <a href="../Variables.htm#amp">address operator (&amp;)</a> to do this:</p>
<pre>SendMessage, 0xC, 0, <strong>&amp;MyVar</strong>, ClassNN, WinTitle  <em>; 0XC is WM_SETTEXT</em></pre>
<p>In v1.0.43.06+, a string put into MyVar by the receiver of the message is properly recognized without the need for extra steps. However, this works only if the parameter's first character is an ampersand (&amp;); for example, <code>5+&amp;MyVar</code> would not work but <code>&amp;MyVar</code> or <code>&amp;MyVar+5</code> would work.</p>
<p>A quoted/literal string may also be sent as in the following working example (the &amp; operator should not be used in this case):</p>
<pre>Run Notepad
WinWait Untitled - Notepad
SendMessage, 0xC, 0, <strong>&quot;New Notepad Title&quot;</strong>  <em>; 0XC is WM_SETTEXT</em></pre>
<p>To send a message to all windows in the system, including those that are hidden or disabled, specify <code>ahk_id 0xFFFF</code> for <em>WinTitle</em> (0xFFFF is HWND_BROADCAST). This technique should be used only for messages intended to be broadcast, such as the following example:</p>
<pre>SendMessage, 0x1A,,,, ahk_id 0xFFFF  <em>; 0x1A is WM_SETTINGCHANGE</em></pre>
<p>To have a script receive a message, use <a href="OnMessage.htm">OnMessage()</a>.</p>
<p>See the <a href="../misc/SendMessage.htm">Message Tutorial</a> for an introduction to using these commands.</p>
<p>Window titles and text are case sensitive. Hidden windows are not detected unless <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a> has been turned on.</p>

<h3>Related</h3>
<p><a href="../misc/SendMessageList.htm">Message List</a>, <a href="../misc/SendMessage.htm">Message Tutorial</a>, <a href="OnMessage.htm">OnMessage()</a>, <a href="../misc/Winamp.htm">Automating Winamp</a>, <a href="DllCall.htm">DllCall</a>, <a href="ControlSend.htm">ControlSend</a>, <a href="WinMenuSelectItem.htm">WinMenuSelectItem</a></p>
<h3 id="Examples">Examples</h3>
<pre class="NoIndent">#o::  <em>; Win+O hotkey that turns off the monitor.</em>
Sleep 1000  <em>; Give user a chance to release keys (in case their release would wake up the monitor again).
; Turn Monitor Off:</em>
SendMessage, 0x112, 0xF170, 2,, Program Manager  <em>; 0x112 is WM_SYSCOMMAND, 0xF170 is SC_MONITORPOWER.
; Note for the above: Use -1 in place of 2 to turn the monitor on.
; Use 1 in place of 2 to activate the monitor's low-power mode.</em>
return

<em>; Start the user's chosen screen saver:</em>
SendMessage, 0x112, 0xF140, 0,, Program Manager  <em>; 0x112 is WM_SYSCOMMAND, and 0xF140 is SC_SCREENSAVE.</em>

<em>; Scroll up by one line (for a control that has a vertical scroll bar):</em>
ControlGetFocus, control, A
SendMessage, 0x115, 0, 0, %control%, A

<em>; Scroll down by one line:</em>
ControlGetFocus, control, A
SendMessage, 0x115, 1, 0, %control%, A

<em>; Switch the active window's keyboard layout/language to English:</em>
PostMessage, 0x50, 0, 0x4090409,, A  <em>; 0x50 is WM_INPUTLANGCHANGEREQUEST.</em>

<em>; This example asks Winamp which track number is currently active:</em>
SetTitleMatchMode, 2
SendMessage, 1024, 0, 120, - Winamp
if ErrorLevel &lt;&gt; FAIL
{
    ErrorLevel++  <em>; Winamp's count starts at &quot;0&quot;, so adjust by 1.</em>
    MsgBox, Track #%ErrorLevel% is active or playing.
}
<em>; See <a href="../misc/Winamp.htm">Automating Winamp</a> for more information.</em>

<em>; To find the process ID of an AHK script (an alternative to &quot;<a href="WinGet.htm">WinGet PID</a>&quot;):</em>
SetTitleMatchMode, 2
DetectHiddenWindows, on
SendMessage, 0x44, 0x405, 0, , SomeOtherScript.ahk - AutoHotkey v
MsgBox %ErrorLevel% is the process id.</pre>

</body>
</html>
